package edu.team7.las.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import edu.team7.las.exception.EmpNotFound;
import edu.team7.las.model.T1Employee;
import edu.team7.las.repository.EmployeeRepository;

@Service
public class EmployeeServiceImpl implements EmployeeService {

	@Resource
	private EmployeeRepository empRepository;
	
	@Override
	@Transactional
	public T1Employee CreateEmp(T1Employee Emp) {
		// TODO Auto-generated method stub
		T1Employee createdEmp = Emp;
		return empRepository.save(createdEmp);
		
	}

	@Override
	@Transactional(rollbackFor=EmpNotFound.class)
	public T1Employee UpdateEmp(T1Employee Emp) throws EmpNotFound {
		// TODO Auto-generated method stub
		T1Employee updatedEmp = empRepository.findOne((long) Emp.getIdEmployee());
		
		if (updatedEmp== null)
			throw new EmpNotFound();
		
		updatedEmp.setDesignation(Emp.getDesignation());
		updatedEmp.setContactNo(Emp.getContactNo());
		updatedEmp.setEmailAddress(Emp.getEmailAddress());
		updatedEmp.setEmpName(Emp.getEmpName());
		updatedEmp.setReportTo(Emp.getReportTo());
		empRepository.flush();
		return updatedEmp;
		
	}

	@Override
	@Transactional(rollbackFor=EmpNotFound.class)
	public T1Employee DeleteEmp(T1Employee Emp) throws EmpNotFound {
		// TODO Auto-generated method stub
		T1Employee deletedEmp = empRepository.findOne((long) Emp.getIdEmployee());
		
		if (deletedEmp== null)
			throw new EmpNotFound();
		empRepository.delete(deletedEmp);
		return deletedEmp;
	}

	@Override
	@Transactional
	public List<T1Employee> fillAll() {
		// TODO Auto-generated method stub
		return empRepository.findAll();
	}

	@Override
	@Transactional
	public T1Employee findByID(int id) {
		// TODO Auto-generated method stub
		return empRepository.findOne((long)id);
	}

}
